package com.healthyeating.manage.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.healthyeating.common.utils.DateUtils;
import com.healthyeating.common.utils.SecurityUtils;
import com.healthyeating.common.utils.bean.BeanUtils;
import com.healthyeating.manage.domain.vo.MenuPlanVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.healthyeating.manage.mapper.MenuPlanMapper;
import com.healthyeating.manage.domain.MenuPlan;
import com.healthyeating.manage.service.IMenuPlanService;

/**
 * 菜单计划Service业务层处理
 *
 * @author 段奇乐
 * @date 2025-03-14
 */
@Service
public class MenuPlanServiceImpl implements IMenuPlanService {

    @Autowired
    private MenuPlanMapper menuPlanMapper;

    /**
     * 查询菜单计划
     *
     * @param menuPlanId 菜单计划主键
     * @return 菜单计划
     */
    @Override
    public MenuPlanVo selectMenuPlanByMenuPlanId(Long menuPlanId) {
        MenuPlan menuPlan = menuPlanMapper.selectMenuPlanByMenuPlanId(menuPlanId);
        List<String> rangeTime = new ArrayList<>();
        MenuPlanVo menuPlanVo = new MenuPlanVo();
        if (menuPlan != null) {
            BeanUtils.copyProperties(menuPlan, menuPlanVo);
            rangeTime.add(DateUtils.dateTime(menuPlan.getStartTime()));
            rangeTime.add(DateUtils.dateTime(menuPlan.getEndTime()));
            menuPlanVo.setRangeTime(rangeTime);
        }
        return menuPlanVo;
    }

    /**
     * 查询菜单计划列表
     *
     * @param menuPlanVo 菜单计划
     * @return 菜单计划
     */
    @Override
    public List<MenuPlan> selectMenuPlanList(MenuPlanVo menuPlanVo) {
        if (menuPlanVo.getRangeTime() != null) {
            List<String> rangeTime = menuPlanVo.getRangeTime();
            menuPlanVo.setStartTime(DateUtils.parseDate(rangeTime.get(0)));
            menuPlanVo.setEndTime(DateUtils.parseDate(rangeTime.get(1)));
        }
        menuPlanVo.setUserId(SecurityUtils.getUserId());
        return menuPlanMapper.selectMenuPlanList(menuPlanVo);
    }

    /**
     * 新增菜单计划
     *
     * @param menuPlanVo 菜单计划
     * @return 结果
     */
    @Override
    public int insertMenuPlan(MenuPlanVo menuPlanVo) {
        menuPlanVo.setCreateTime(DateUtils.getNowDate());
        List<String> rangeTime = menuPlanVo.getRangeTime();
        menuPlanVo.setStartTime(DateUtils.parseDate(rangeTime.get(0)));
        menuPlanVo.setEndTime(DateUtils.parseDate(rangeTime.get(1)));
        menuPlanVo.setUserId(SecurityUtils.getUserId());
        return menuPlanMapper.insertMenuPlan(menuPlanVo);
    }

    /**
     * 修改菜单计划
     *
     * @param menuPlan 菜单计划
     * @return 结果
     */
    @Override
    public int updateMenuPlan(MenuPlan menuPlan) {
        return menuPlanMapper.updateMenuPlan(menuPlan);
    }

    /**
     * 批量删除菜单计划
     *
     * @param menuPlanIds 需要删除的菜单计划主键
     * @return 结果
     */
    @Override
    public int deleteMenuPlanByMenuPlanIds(Long[] menuPlanIds) {
        return menuPlanMapper.deleteMenuPlanByMenuPlanIds(menuPlanIds);
    }

    /**
     * 删除菜单计划信息
     *
     * @param menuPlanId 菜单计划主键
     * @return 结果
     */
    @Override
    public int deleteMenuPlanByMenuPlanId(Long menuPlanId) {
        return menuPlanMapper.deleteMenuPlanByMenuPlanId(menuPlanId);
    }
}
